package com.changhai.diabetes.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.changhai.diabetes.model.Drug;
import com.changhai.diabetes.model.DrugSuifang;
import com.changhai.diabetes.model.DrugSurvey;
import com.changhai.diabetes.model.DrugTreatment;
import com.changhai.diabetes.service.DrugService;
import com.changhai.diabetes.util.Constants;

/**
 * 药物治疗
 * 
 * @author Andy
 * 
 */
public class DrugServiceImpl extends CommonServiceImpl implements DrugService {
	public DrugTreatment searchDrugTreatByPatientId(Long patientid) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "drugTreatment.getByPatientId", patientid);
		if (o != null && o instanceof DrugTreatment) {
			return (DrugTreatment) o;
		} else {
			DrugTreatment d = new DrugTreatment();
			d.setPatientid(patientid);
			return d;
		}
	}

	public void saveEditDrugTreat(DrugTreatment drugTreatment) {
		if (drugTreatment != null) {
			if (drugTreatment.getSeqid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "drugTreatment.updateByPrimaryKey", drugTreatment);
			} else {
				Long id = this.saveNewDrugTreat(drugTreatment);
				if (id != null && id > 0) {
					drugTreatment.setSeqid(id);
					this.updateObject(Constants.DS_CHANGHAI, "drugTreatment.updateByPrimaryKey", drugTreatment);
				}
			}
		}
	}

	public Long saveNewDrugTreat(DrugTreatment drugTreatment) {
		Integer id = 0;
		if (drugTreatment != null && drugTreatment.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "drugTreatment.insert", drugTreatment);
		}
		return new Long(id);
	}

	public DrugSurvey searchDrugSurveyByPatientId(Long patientid) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "drugSurvey.getByPatientId", patientid);
		if (o != null && o instanceof DrugSurvey) {
			return (DrugSurvey) o;
		} else {
			DrugSurvey d = new DrugSurvey();
			d.setPatientid(patientid);
			return d;
		}
	}

	public void saveEditDrugSurvey(DrugSurvey drugSurvey) {
		if (drugSurvey != null) {
			if (drugSurvey.getSeqid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "drugSurvey.updateByPrimaryKey", drugSurvey);
			} else {
				Long id = this.saveNewDrugSurvey(drugSurvey);
				if (id != null && id > 0) {
					drugSurvey.setSeqid(id);
					this.updateObject(Constants.DS_CHANGHAI, "drugSurvey.updateByPrimaryKey", drugSurvey);
				}
			}
		}
	}

	public Long saveNewDrugSurvey(DrugSurvey drugSurvey) {
		Integer id = 0;
		if (drugSurvey != null && drugSurvey.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "drugSurvey.insert", drugSurvey);
		}
		return new Long(id);
	}
	
	public DrugSuifang searchDrugSuifangByPatientId(Long patientid) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "drugSuifang.getByPatientId", patientid);
		if (o != null && o instanceof DrugSuifang) {
			return (DrugSuifang) o;
		} else {
			DrugSuifang d = new DrugSuifang();
			d.setPatientid(patientid);
			return d;
		}
	}

	public void saveEditDrugSuifang(DrugSuifang drugSuifang) {
		if (drugSuifang != null) {
			if (drugSuifang.getSeqid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "drugSuifang.updateByPrimaryKey", drugSuifang);
			} else {
				Long id = this.saveNewDrugSuifang(drugSuifang);
				if (id != null && id > 0) {
					drugSuifang.setSeqid(id);
					this.updateObject(Constants.DS_CHANGHAI, "drugSuifang.updateByPrimaryKey", drugSuifang);
				}
			}
		}
	}

	public Long saveNewDrugSuifang(DrugSuifang drugSuifang) {
		Integer id = 0;
		if (drugSuifang != null && drugSuifang.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "drugSuifang.insert", drugSuifang);
		}
		return new Long(id);
	}
	
	public List<Drug> searchDrugList() {
		List<Drug> list = new ArrayList<Drug>();
		list = this.getList(Constants.DS_CHANGHAI, "drug.getAllDrugs", null);
		return list;
	}
}
